# -*- coding: utf-8 -*-
# @Time    : 2025/10/9
# @Author  : Qwen
# @File    : baidu_map_service.py
# @Describe: 百度地图路线规划服务

import requests
import json
import logging
from typing import Dict, Any
from luck.core.LuckConstant import LuckConstant

class MapSdkBaiduService:
    def __init__(self, ak: str):
        # 百度地图API配置
        self.ak = ak
        self.base_url = "https://api.map.baidu.com"
    
    def get_route_info(self, origin: str, destination: str, route_type: str = "driving", tactics: str = "0") -> Dict[str, Any]:
        """
        获取路线规划信息
        
        Args:
            origin: 起点坐标 (纬度,经度)
            destination: 终点坐标 (纬度,经度)
            route_type: 路线类型 (driving, walking, riding)
            tactics: 路线偏好
            
        Returns:
            dict: 路线信息
        """
        try:
            # 根据路线类型选择API接口
            if route_type == "driving":
                api_url = f"{self.base_url}/direction/v2/driving"
                params = {
                    "origin": origin,
                    "destination": destination,
                    "ak": self.ak,
                    "tactics": tactics,  # 驾车路线偏好
                    "ret_fields": "toll,toll_distance,traffic_light"  # 添加道路收费、收费路段里程和红绿灯数量字段
                }
            elif route_type == "walking":
                api_url = f"{self.base_url}/direction/v2/walking"
                params = {
                    "origin": origin,
                    "destination": destination,
                    "ak": self.ak
                }
            elif route_type == "riding":
                api_url = f"{self.base_url}/direction/v2/riding"
                params = {
                    "origin": origin,
                    "destination": destination,
                    "ak": self.ak
                }
            else:
                logging.warning(f"不支持的路线类型: {route_type}")
                return {
                    "code": LuckConstant.CODE_FAIL,
                    "msg": "不支持的路线类型"
                }
            
            logging.info(f"请求百度地图API: {api_url}, 参数: {params}")
            
            # 发送请求
            response = requests.get(api_url, params=params, timeout=10)
            
            if response.status_code == 200:
                result = response.json()
                logging.info(f"百度地图API响应状态: {result.get('status')}")
                
                # 检查百度地图API返回状态
                if result.get("status") == 0:
                    return {
                        "code": LuckConstant.CODE_SUCCESS,
                        "msg": LuckConstant.MSG_SUCCESS,
                        "data": result
                    }
                else:
                    error_msg = f"百度地图API错误: {result.get('message', '未知错误')}"
                    logging.error(error_msg)
                    return {
                        "code": LuckConstant.CODE_FAIL,
                        "msg": error_msg
                    }
            else:
                error_msg = f"请求失败，状态码: {response.status_code}"
                logging.error(error_msg)
                return {
                    "code": LuckConstant.CODE_FAIL,
                    "msg": error_msg
                }
                
        except requests.exceptions.Timeout:
            error_msg = "请求百度地图API超时"
            logging.error(error_msg)
            return {
                "code": LuckConstant.CODE_FAIL,
                "msg": error_msg
            }
        except requests.exceptions.RequestException as e:
            error_msg = f"网络请求错误: {str(e)}"
            logging.error(error_msg)
            return {
                "code": LuckConstant.CODE_FAIL,
                "msg": error_msg
            }
        except Exception as e:
            error_msg = f"服务处理错误: {str(e)}"
            logging.error(error_msg)
            return {
                "code": LuckConstant.CODE_FAIL,
                "msg": error_msg
            }